www.gusucode.com > 超声波测量以及形成图像 对相关信号进行模拟仿真 > 超声波测量以及形成图像 对相关信号进行模拟仿真/digital holograpy/dh/orm_window.m

    function varargout = orm_window(varargin)
% ORM_WINDOW M-file for orm_window.fig
%      ORM_WINDOW, by itself, creates a new ORM_WINDOW or raises the existing
%      singleton*.
%
%      H = ORM_WINDOW returns the handle to a new ORM_WINDOW or the handle to
%      the existing singleton*.
%
%      ORM_WINDOW('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ORM_WINDOW.M with the given input arguments.
%
%      ORM_WINDOW('Property','Value',...) creates a new ORM_WINDOW or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before orm_window_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to orm_window_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help orm_window

% Last Modified by GUIDE v2.5 25-Dec-2007 15:17:26

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @orm_window_OpeningFcn, ...
                   'gui_OutputFcn',  @orm_window_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before orm_window is made visible.
function orm_window_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to orm_window (see VARARGIN)
axes(handles.axes)
imagesc(ones(256)),colormap(gray);

set(handles.phase_lbox,'String',cell(1,0),'value',1);
set(handles.region_lbox,'String',cell(1,0),'value',1);

handles.fs=2;
handles.phase=[];
handles.ci=[];
handles.cr=[];

handles.brushsz=[];
handles.brushszdisp=[];
handles.pointer=[];
handles.ratio=[];
handles.copy=[];

if nargin > 4
    for n=1:2:length(varargin)
        if strcmpi(varargin{n},'phasemaps')
            handles.phase=varargin{n+1};
            if ~isempty(handles.phase)
                items1=sort(fieldnames(handles.phase));
                items2=repmat({'none'},[length(items1),1]);
                for k=1:length(items1)
                    if ~isempty(handles.phase(4).(items1{k}))
                        items2{k}=[num2str(size(handles.phase(4).(items1{k}),1)),...
                                '×',num2str(size(handles.phase(4).(items1{k}),2))];
                    end
                end
                set(handles.phase_lbox,'string',items1)
                set(handles.region_lbox,'string',items2)
            end
        end
    end
end

% Choose default command line output for orm_window
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes orm_window wait for user response (see UIRESUME)
uiwait(handles.orm_window);


% --- Outputs from this function are returned to the command line.
function varargout = orm_window_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
if isempty(findobj('type','figure','tag','orm_window'))
    varargout{1}=[];
    return
end

if strcmp(get(handles.done_button,'enable'),'off')
    varargout{1}=handles.phase;
else
    varargout{1}=[];
end

delete(handles.orm_window)


% --- Executes on button press in up_button.
function up_button_Callback(hObject, eventdata, handles)
% hObject    handle to up_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
items1=get(handles.phase_lbox,'string');
items2=get(handles.region_lbox,'string');
index=get(handles.phase_lbox,'value');
if isempty(items1) || length(items1)==1 || (max(index)-min(index)+1)>length(index)
    return
end

if min(index)>1
    temp=items1(min(index)-1);
    items1(index-1)=items1(index);
    items1(max(index))=temp;
    temp=items2(min(index)-1);
    items2(index-1)=items2(index);
    items2(max(index))=temp;
    set(handles.phase_lbox,'string',items1);
    set(handles.region_lbox,'string',items2);
    set(handles.phase_lbox,'value',index-1);
    set(handles.region_lbox,'value',index-1);
end


% --- Executes on button press in down_button.
function down_button_Callback(hObject, eventdata, handles)
% hObject    handle to down_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
items1=get(handles.phase_lbox,'string');
items2=get(handles.region_lbox,'string');
index=get(handles.phase_lbox,'value');
if isempty(items1) || length(items1)==1 || (max(index)-min(index)+1)>length(index)
    return
end

if max(index)<length(items1)
    temp=items1(max(index)+1);
    items1(index+1)=items1(index);
    items1(min(index))=temp;
    temp=items2(max(index)+1);
    items2(index+1)=items2(index);
    items2(min(index))=temp;
    set(handles.phase_lbox,'string',items1);
    set(handles.region_lbox,'string',items2);
    set(handles.phase_lbox,'value',index+1);
    set(handles.region_lbox,'value',index+1);
end


% --- Executes on button press in goright_button.
function goright_button_Callback(hObject, eventdata, handles)
% hObject    handle to goright_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
items=get(handles.phase_lbox,'string');
if isempty(items)
    return
end
index=get(handles.phase_lbox,'value');

sz=handles.phase(2).(items{index(1)}).MN2;
for n=index(2:end)
    if ~isequal(handles.phase(2).(items{n}).MN2,sz)
        errordlg('Phase maps selected are not of the same size','Error','modal');
        return
    end
end

handles.ci=zeros(sz);
t=[];
for n=index
    handles.ci=handles.ci+handles.phase(3).(items{n}).weight;
    if ~isempty(handles.phase(4).(items{n}))
        t=[t,n];
    end
end
handles.ci=zero2one(handles.ci);
if isempty(t)
    handles.cr=handles.ci;
else
    handles.cr=handles.phase(4).(items{t(1)});
    if length(t)>1
        warndlg('More than one item you selected have a region matrix, the first one is adopted','Warning')
    end
end

% adjust the axes
ap=[461,26,399,308];
set(handles.axes,'position',ap)
imsize=fliplr(size(handles.cr));
handles.ratio=min(ap(3:4)./imsize);
newsize=handles.ratio.*imsize;
add=round((ap(3:4)-newsize)./2);
ap=[ap(1:2)+add,round(newsize)];
set(handles.axes,'position',ap)
%

brushsize_popup_Callback(handles.brushsize_popup, [], handles)

axes(handles.axes)
if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
    imagesc(handles.cr),colormap(gray);
else
    icv=get(handles.ic_slider,'value');
    imagesc(handles.ci.^icv),colormap(gray);
end
axis equal tight

set(handles.contrast_slider,'value',1)
set(handles.threshold1_slider,'value',0.5)
set(handles.threshold2_slider,'value',0.5)

if islogical(handles.cr)
    str='off';
else
    str='on';
end
set(handles.contrast_slider,'enable',str)
set(handles.contrastok_button,'enable',str)
set(handles.contrastcancel_button,'enable',str)
set(handles.threshold1_slider,'enable',str)
set(handles.threshold2_slider,'enable',str)
set(handles.thresholdok_button,'enable',str)
set(handles.thresholdcancel_button,'enable',str)

set(handles.filterok_button,'enable','on')

set(handles.middle_frame1,'backgroundcolor',[0.6,0.6,0.6])
set(handles.contrast_text,'backgroundcolor',[0.6,0.6,0.6])
set(handles.middle_frame3,'backgroundcolor',[0.6,0.6,0.6])
set(handles.threshold_text,'backgroundcolor',[0.6,0.6,0.6])
set(handles.lr_radio,'backgroundcolor',[0.6,0.6,0.6])
set(handles.ud_radio,'backgroundcolor',[0.6,0.6,0.6])


% --- Executes during object creation, after setting all properties.
function phase_lbox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to phase_lbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in phase_lbox.
function phase_lbox_Callback(hObject, eventdata, handles)
% hObject    handle to phase_lbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns phase_lbox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from phase_lbox
set(handles.region_lbox,'value',get(hObject,'value'))


% --- Executes during object creation, after setting all properties.
function region_lbox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to region_lbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in region_lbox.
function region_lbox_Callback(hObject, eventdata, handles)
% hObject    handle to region_lbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns region_lbox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from region_lbox
set(handles.phase_lbox,'value',get(hObject,'value'))


% --- Executes on button press in clear_button.
function clear_button_Callback(hObject, eventdata, handles)
% hObject    handle to clear_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
items1=get(handles.phase_lbox,'string');
items2=get(handles.region_lbox,'string');
index=get(handles.phase_lbox,'value');
if isempty(items1)
    return
end

for n=index
    handles.phase(4).(items1{n})=[];
    items2{n}='none';
end

set(handles.region_lbox,'string',items2)

guidata(hObject,handles)


% --- Executes on button press in goleft_button.
function goleft_button_Callback(hObject, eventdata, handles)
% hObject    handle to goleft_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isempty(handles.cr) || ~islogical(handles.cr)
    warndlg('The current region matrix is not ready, nothing is done','Warning')
    return
end

items1=get(handles.phase_lbox,'string');
items2=get(handles.region_lbox,'string');
index=get(handles.phase_lbox,'value');

crsz=size(handles.cr);
str=[num2str(crsz(1)),'×',num2str(crsz(2))];
unmatched=0;
for n=index
    if isequal(handles.phase(2).(items1{n}).MN2,crsz)
        handles.phase(4).(items1{n})=handles.cr;
        items2{n}=str;
        if get(handles.apply_check,'value')==get(handles.apply_check,'max')
            handles.phase(1).(items1{n})=handles.phase(1).(items1{n}).*handles.cr;
        end
    else
        unmatched=1;
    end
end

if unmatched
    warndlg('Can not give an unmatched region matrix to a phase map','Warning')
end

set(handles.region_lbox,'string',items2);

guidata(hObject,handles)


% --- Executes on button press in intensity_toggle.
function intensity_toggle_Callback(hObject, eventdata, handles)
% hObject    handle to intensity_toggle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of intensity_toggle
if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
    set(hObject,'value',get(hObject,'max'))
    set(handles.region_toggle,'value',get(handles.region_toggle,'min'))
    if ~isempty(handles.ci)
        icv=get(handles.ic_slider,'value');
        imagesc(handles.ci.^icv),colormap(gray);
        axis equal tight
    end
else
    set(hObject,'value',get(hObject,'max'))
end


% --- Executes on button press in region_toggle.
function region_toggle_Callback(hObject, eventdata, handles)
% hObject    handle to region_toggle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of region_toggle
if get(handles.intensity_toggle,'value')==get(handles.intensity_toggle,'max')
    set(hObject,'value',get(hObject,'max'))
    set(handles.intensity_toggle,'value',get(handles.intensity_toggle,'min'))
    if ~isempty(handles.cr)
        axes(handles.axes)
        if isequal(get(handles.middle_frame1,'backgroundcolor'),[1,0,0])
            cv=get(handles.contrast_slider,'value');
            imagesc((handles.cr).^cv),colormap(gray);
        elseif isequal(get(handles.middle_frame3,'backgroundcolor'),[1,0,0])
            tv1=get(handles.threshold1_slider,'value');
            tv2=get(handles.threshold2_slider,'value');
            tvd=(get(handles.lr_radio,'value')==get(handles.lr_radio,'max'));
            if tvd
                imagesc(handles.cr>repmat(linspace(tv1,tv2,size(handles.cr,2)),size(handles.cr,1),1)),colormap(gray);
            else
                imagesc(handles.cr>repmat(linspace(tv1,tv2,size(handles.cr,1))',1,size(handles.cr,2))),colormap(gray);
            end
        else
            imagesc(handles.cr),colormap(gray);
        end
        axis equal tight
    end
else
    set(hObject,'value',get(hObject,'max'))
end


% --- Executes during object creation, after setting all properties.
function contrast_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to contrast_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function contrast_slider_Callback(hObject, eventdata, handles)
% hObject    handle to contrast_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
if isempty(handles.cr)
    return
end

if isequal(get(handles.middle_frame1,'backgroundcolor'),[0.6,0.6,0.6])
    set(handles.middle_frame1,'backgroundcolor',[1,0,0])
    set(handles.contrast_text,'backgroundcolor',[1,0,0])
    set(handles.filterok_button,'enable','off')
    set(handles.threshold1_slider,'enable','off')
    set(handles.threshold2_slider,'enable','off')
    set(handles.thresholdok_button,'enable','off')
    set(handles.thresholdcancel_button,'enable','off')
end

if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
    cv=get(hObject,'value');
    axes(handles.axes)
    imagesc((handles.cr).^cv),colormap(gray);
    axis equal tight
end


% --- Executes during object creation, after setting all properties.
function filtersize_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to filtersize_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function filtersize_edit_Callback(hObject, eventdata, handles)
% hObject    handle to filtersize_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of filtersize_edit as text
%        str2double(get(hObject,'String')) returns contents of filtersize_edit as a double
fs=round(abs(str2num(get(hObject,'string'))));
if isempty(fs) || length(fs)>1 || fs==0
    fs=3;
end
set(hObject,'string',num2str(fs));


% --- Executes on button press in filterok_button.
function filterok_button_Callback(hObject, eventdata, handles)
% hObject    handle to filterok_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fs=str2num(get(handles.filtersize_edit,'string'));
if isempty(handles.cr) || fs==1
    return
end

handles.cr=zero2one(conv2(double(handles.cr),ones(fs),'same'));

if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
    axes(handles.axes)
    imagesc(handles.cr),colormap(gray);
    axis equal tight
end

set(handles.contrast_slider,'enable','on')
set(handles.contrastok_button,'enable','on')
set(handles.contrastcancel_button,'enable','on')
set(handles.threshold1_slider,'enable','on')
set(handles.threshold2_slider,'enable','on')
set(handles.thresholdok_button,'enable','on')
set(handles.thresholdcancel_button,'enable','on')

guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function threshold1_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to threshold1_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function threshold1_slider_Callback(hObject, eventdata, handles)
% hObject    handle to threshold1_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
if isempty(handles.cr)
    return
end

if isequal(get(handles.middle_frame3,'backgroundcolor'),[0.6,0.6,0.6])
    set(handles.middle_frame3,'backgroundcolor',[1,0,0])
    set(handles.threshold_text,'backgroundcolor',[1,0,0])
    set(handles.lr_radio,'backgroundcolor',[1,0,0])
    set(handles.ud_radio,'backgroundcolor',[1,0,0])
    set(handles.filterok_button,'enable','off')
    set(handles.contrast_slider,'enable','off')
    set(handles.contrastok_button,'enable','off')
    set(handles.contrastcancel_button,'enable','off')
end

if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
    tv1=get(hObject,'value');
    tv2=get(handles.threshold2_slider,'value');
    tvd=(get(handles.lr_radio,'value')==get(handles.lr_radio,'max'));
    if tvd
        tvarray=repmat(linspace(tv1,tv2,size(handles.cr,2)),size(handles.cr,1),1);
    else
        tvarray=repmat(linspace(tv1,tv2,size(handles.cr,1))',1,size(handles.cr,2));
    end
    axes(handles.axes)
    imagesc(handles.cr>tvarray),colormap(gray);
    axis equal tight
end




% --- Executes on button press in contrastok_button.
function contrastok_button_Callback(hObject, eventdata, handles)
% hObject    handle to contrastok_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isequal(get(handles.middle_frame1,'backgroundcolor'),[1,0,0])
    return
end

cv=get(handles.contrast_slider,'value');
handles.cr=handles.cr.^cv;

set(handles.middle_frame1,'backgroundcolor',[0.6,0.6,0.6])
set(handles.contrast_text,'backgroundcolor',[0.6,0.6,0.6])
set(handles.contrast_slider,'value',1)

set(handles.threshold1_slider,'enable','on')
set(handles.threshold2_slider,'enable','on')
set(handles.thresholdok_button,'enable','on')
set(handles.thresholdcancel_button,'enable','on')
set(handles.filterok_button,'enable','on')

guidata(hObject,handles)


% --- Executes on button press in thresholdok_button.
function thresholdok_button_Callback(hObject, eventdata, handles)
% hObject    handle to thresholdok_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isequal(get(handles.middle_frame3,'backgroundcolor'),[1,0,0])
    return
end

tv1=get(handles.threshold1_slider,'value');
tv2=get(handles.threshold2_slider,'value');
tvd=(get(handles.lr_radio,'value')==get(handles.lr_radio,'max'));
if tvd
    tvarray=repmat(linspace(tv1,tv2,size(handles.cr,2)),size(handles.cr,1),1);
else
    tvarray=repmat(linspace(tv1,tv2,size(handles.cr,1))',1,size(handles.cr,2));
end
handles.cr=handles.cr>tvarray;

set(handles.middle_frame3,'backgroundcolor',[0.6,0.6,0.6])
set(handles.threshold_text,'backgroundcolor',[0.6,0.6,0.6])
set(handles.lr_radio,'backgroundcolor',[0.6,0.6,0.6])
set(handles.ud_radio,'backgroundcolor',[0.6,0.6,0.6])
set(handles.threshold1_slider,'value',0.5)
set(handles.threshold2_slider,'value',0.5)

set(handles.threshold1_slider,'enable','off')
set(handles.threshold2_slider,'enable','off')
set(handles.thresholdok_button,'enable','off')
set(handles.thresholdcancel_button,'enable','off')
set(handles.filterok_button,'enable','on')
set(handles.contrast_slider,'enable','off')
set(handles.contrastok_button,'enable','off')
set(handles.contrastcancel_button,'enable','off')

guidata(hObject,handles)


% --- Executes on button press in done_button.
function done_button_Callback(hObject, eventdata, handles)
% hObject    handle to done_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isequal(get(handles.orm_window, 'waitstatus'), 'waiting')
    % The GUI is still in UIWAIT, us UIRESUME
    set(hObject,'enable','off')
    uiresume(handles.orm_window);
else
    % The GUI is no longer waiting, just close it
    delete(handles.orm_window);
end


% --- Executes on button press in paintbrush_toggle.
function paintbrush_toggle_Callback(hObject, eventdata, handles)
% hObject    handle to paintbrush_toggle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of paintbrush_toggle


% --- Executes during object creation, after setting all properties.
function brushsize_popup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to brushsize_popup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in brushsize_popup.
function brushsize_popup_Callback(hObject, eventdata, handles)
% hObject    handle to brushsize_popup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns brushsize_popup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from brushsize_popup
items=get(hObject,'string');
index=get(hObject,'value');
handles.brushsz=str2num(items{index});
if ~isempty(handles.ratio)
    color=get(handles.color_frame,'backgroundcolor');
    color=color(1)+1;
    handles.brushszdisp=min(max(1,round(handles.brushsz.*handles.ratio)),16);
    handles.pointer=paste(zeros(16).*NaN,ones(handles.brushszdisp).*color);
end

guidata(hObject,handles)


% --- Executes on button press in apply_check.
function apply_check_Callback(hObject, eventdata, handles)
% hObject    handle to apply_check (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of apply_check


% --- Executes on button press in copy_button.
function copy_button_Callback(hObject, eventdata, handles)
% hObject    handle to copy_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
items1=get(handles.phase_lbox,'string');
items2=get(handles.region_lbox,'string');
index=get(handles.phase_lbox,'value');
if isempty(items1)
    return
end

if length(index)>1
    warndlg('Only one item can be copied','Warning')
    return
end

items1=items1(index);
items2=items2(index);
handles.copy=[items1,items2];

guidata(hObject,handles)


% --- Executes on button press in paste_button.
function paste_button_Callback(hObject, eventdata, handles)
% hObject    handle to paste_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isempty(handles.copy)
    return
end

items1=get(handles.phase_lbox,'string');
items2=get(handles.region_lbox,'string');
index=get(handles.phase_lbox,'value');

unmatched=0;
for n=index
    if isequal(handles.phase(2).(items1{n}).MN2,handles.phase(2).(handles.copy{1}).MN2)
        handles.phase(4).(items1{n})=handles.phase(4).(handles.copy{1});
        items2(n)=handles.copy(2);
        if get(handles.apply_check,'value')==get(handles.apply_check,'max')
            handles.phase(1).(items1{n})=handles.phase(1).(items1{n}).*handles.phase(4).(items1{n});
        end
    else
        unmatched=1;
    end
end

if unmatched
    warndlg('Can not copy an unmatched region matrix to a phase map','Warning')
end

set(handles.region_lbox,'string',items2)

guidata(hObject,handles)


% --- Executes on button press in sall_button.
function sall_button_Callback(hObject, eventdata, handles)
% hObject    handle to sall_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
items=get(handles.phase_lbox,'string');
if isempty(items)
    return
end
set(handles.phase_lbox,'value',1:length(items))
set(handles.region_lbox,'value',1:length(items))


% --- Executes on button press in i2r_button.
function i2r_button_Callback(hObject, eventdata, handles)
% hObject    handle to i2r_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of i2r_button
if ~isempty(handles.ci)
    handles.cr=handles.ci;
    if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
        axes(handles.axes)
        imagesc(handles.cr),colormap(gray);
        axis equal tight
    end
    set(handles.contrast_slider,'enable','on')
    set(handles.contrastok_button,'enable','on')
    set(handles.contrastcancel_button,'enable','on')
    set(handles.threshold1_slider,'enable','on')
    set(handles.threshold2_slider,'enable','on')
    set(handles.thresholdok_button,'enable','on')
    set(handles.thresholdcancel_button,'enable','on')
    set(handles.filterok_button,'enable','on')
    
    set(handles.contrast_slider,'value',1)
    set(handles.threshold1_slider,'value',0.5)
    set(handles.threshold2_slider,'value',0.5)
    
    set(handles.middle_frame1,'backgroundcolor',[0.6,0.6,0.6])
    set(handles.contrast_text,'backgroundcolor',[0.6,0.6,0.6])
    set(handles.middle_frame3,'backgroundcolor',[0.6,0.6,0.6])
    set(handles.threshold_text,'backgroundcolor',[0.6,0.6,0.6])
    set(handles.lr_radio,'backgroundcolor',[0.6,0.6,0.6])
    set(handles.ud_radio,'backgroundcolor',[0.6,0.6,0.6])
    guidata(hObject,handles)
end


% --- Executes on button press in contrastcancel_button.
function contrastcancel_button_Callback(hObject, eventdata, handles)
% hObject    handle to contrastcancel_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isequal(get(handles.middle_frame1,'backgroundcolor'),[1,0,0])
    return
end

set(handles.middle_frame1,'backgroundcolor',[0.6,0.6,0.6])
set(handles.contrast_text,'backgroundcolor',[0.6,0.6,0.6])
set(handles.contrast_slider,'value',1)

set(handles.threshold1_slider,'enable','on')
set(handles.threshold2_slider,'enable','on')
set(handles.thresholdok_button,'enable','on')
set(handles.thresholdcancel_button,'enable','on')
set(handles.filterok_button,'enable','on')

if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
    axes(handles.axes)
    imagesc(handles.cr),colormap(gray);
    axis equal tight
end


% --- Executes on button press in thresholdcancel_button.
function thresholdcancel_button_Callback(hObject, eventdata, handles)
% hObject    handle to thresholdcancel_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isequal(get(handles.middle_frame3,'backgroundcolor'),[1,0,0])
    return
end

set(handles.middle_frame3,'backgroundcolor',[0.6,0.6,0.6])
set(handles.threshold_text,'backgroundcolor',[0.6,0.6,0.6])
set(handles.lr_radio,'backgroundcolor',[0.6,0.6,0.6])
set(handles.ud_radio,'backgroundcolor',[0.6,0.6,0.6])
set(handles.threshold1_slider,'value',0.5)
set(handles.threshold2_slider,'value',0.5)

set(handles.contrast_slider,'enable','on')
set(handles.contrastok_button,'enable','on')
set(handles.contrastcancel_button,'enable','on')
set(handles.filterok_button,'enable','on')

if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
    axes(handles.axes)
    imagesc(handles.cr),colormap(gray);
    axis equal tight
end


% --- Executes during object creation, after setting all properties.
function ic_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ic_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function ic_slider_Callback(hObject, eventdata, handles)
% hObject    handle to ic_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
if isempty(handles.ci)
    return
end

if get(handles.intensity_toggle,'value')==get(handles.intensity_toggle,'max')
    icv=get(handles.ic_slider,'value');
    axes(handles.axes)
    imagesc(handles.ci.^icv),colormap(gray);
    axis equal tight
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over color_frame.
function color_frame_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to color_frame (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
color=get(handles.color_frame,'backgroundcolor');
set(handles.color_frame,'backgroundcolor',[1,1,1]-color)
if ~isempty(handles.pointer)
    handles.pointer=ones(16).*3-handles.pointer;
end

guidata(hObject,handles)


% --- Executes on mouse motion over figure - except title and menu.
function orm_window_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to orm_window (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ap=get(handles.axes,'position');
pp=get(handles.orm_window,'currentpoint');
if ~isempty(handles.cr)...
        && get(handles.paintbrush_toggle,'value')==get(handles.paintbrush_toggle,'max')...
        && get(handles.region_toggle,'value')==get(handles.region_toggle,'max')...
        && pp(1)>=ap(1) && pp(1)<ap(1)+ap(3)...
        && pp(2)>=ap(2) && pp(2)<ap(2)+ap(4)
    set(handles.orm_window,'pointer','custom')
    set(handles.orm_window,'pointershapehotspot',[9,9])
    set(handles.orm_window,'pointershapecdata',handles.pointer)
else
    set(handles.orm_window,'pointer','arrow')
end


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function orm_window_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to orm_window (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ap=get(handles.axes,'position');
pp=get(handles.orm_window,'currentpoint');
if ~isempty(handles.cr)...
        && get(handles.paintbrush_toggle,'value')==get(handles.paintbrush_toggle,'max')...
        && get(handles.region_toggle,'value')==get(handles.region_toggle,'max')...
        && pp(1)>=ap(1) && pp(1)<ap(1)+ap(3)...
        && pp(2)>=ap(2) && pp(2)<ap(2)+ap(4)...
        && ~isequal(get(handles.middle_frame1,'backgroundcolor'),[1,0,0])...
        && ~isequal(get(handles.middle_frame3,'backgroundcolor'),[1,0,0])
    color=get(handles.color_frame,'backgroundcolor');
    color=color(1);
    xlim=get(handles.axes,'xlim');
    ylim=get(handles.axes,'ylim');
    center=round([ylim(2)-(pp(2)-ap(2)+0.5)./ap(4).*(ylim(2)-ylim(1)),xlim(1)+(pp(1)-ap(1)+0.5)./ap(3).*(xlim(2)-xlim(1))]);
    [M,N]=size(handles.cr);
    center=min(max(center,[1,1]),[M,N]);
    lu=round((handles.brushsz-1)/2);
    rd=handles.brushsz-1-lu;
    left=max(1,center(2)-lu);
    right=min(N,center(2)+rd);
    up=max(1,center(1)-lu);
    down=min(M,center(1)+rd);
    handles.cr(up:down,left:right)=color;
    axes(handles.axes)
    imagesc(handles.cr),colormap(gray);
    axis equal tight
    guidata(hObject,handles)
end
guidata(hObject,handles)


% --- Executes when user attempts to close orm_window.
function orm_window_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to orm_window (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
if isequal(get(handles.orm_window, 'waitstatus'), 'waiting')
    % The GUI is still in UIWAIT, us UIRESUME
    uiresume(handles.orm_window);
else
    % The GUI is no longer waiting, just close it
    delete(handles.orm_window);
end


% --- Executes during object creation, after setting all properties.
function threshold2_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to threshold2_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function threshold2_slider_Callback(hObject, eventdata, handles)
% hObject    handle to threshold2_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
if isempty(handles.cr)
    return
end

if isequal(get(handles.middle_frame3,'backgroundcolor'),[0.6,0.6,0.6])
    set(handles.middle_frame3,'backgroundcolor',[1,0,0])
    set(handles.threshold_text,'backgroundcolor',[1,0,0])
    set(handles.lr_radio,'backgroundcolor',[1,0,0])
    set(handles.ud_radio,'backgroundcolor',[1,0,0])
    set(handles.filterok_button,'enable','off')
    set(handles.contrast_slider,'enable','off')
    set(handles.contrastok_button,'enable','off')
    set(handles.contrastcancel_button,'enable','off')
end

if get(handles.region_toggle,'value')==get(handles.region_toggle,'max')
    tv1=get(handles.threshold1_slider,'value');
    tv2=get(hObject,'value');
    tvd=(get(handles.lr_radio,'value')==get(handles.lr_radio,'max'));
    if tvd
        tvarray=repmat(linspace(tv1,tv2,size(handles.cr,2)),size(handles.cr,1),1);
    else
        tvarray=repmat(linspace(tv1,tv2,size(handles.cr,1))',1,size(handles.cr,2));
    end
    axes(handles.axes)
    imagesc(handles.cr>tvarray),colormap(gray);
    axis equal tight
end


% --- Executes on button press in lr_radio.
function lr_radio_Callback(hObject, eventdata, handles)
% hObject    handle to lr_radio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of lr_radio
if get(handles.ud_radio,'value')==get(handles.ud_radio,'max')
    set(handles.ud_radio,'value',get(handles.ud_radio,'min'))
else
    set(hObject,'value',get(hObject,'max'))
end


% --- Executes on button press in ud_radio.
function ud_radio_Callback(hObject, eventdata, handles)
% hObject    handle to ud_radio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of ud_radio
if get(handles.lr_radio,'value')==get(handles.lr_radio,'max')
    set(handles.lr_radio,'value',get(handles.lr_radio,'min'))
else
    set(hObject,'value',get(hObject,'max'))
end